package com.sinog.core.util;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @desc request参数转换成map
 * @author -
 * @date -
 */
public final class ConvertHttpToMap {

    /**
     * http请求参数转map函数
     * @param request 请求参数
     * @return 返回Map<String,Object>
     */
    public static Map<String,Object> getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map<String,String[]> properties = request.getParameterMap();
        // 返回值Map
        Map<String,Object> returnMap = new HashMap<>(16);
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name;
        String value = "";
        while(entries.hasNext()) {
            entry = (Map.Entry)entries.next();
            name = (String)entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj) {
                value = "";
            } else if(valueObj instanceof String[]) {
                String[] values = (String[])valueObj;
                for(String s : values) {
                    value = s + ",";
                }
                value = value.substring(0,value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name,value);
        }
        return returnMap;
    }
}